package DataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import pojo.FileSharePOJO;

public class FileShareDA extends BaseDA {

	@Override
	protected FileSharePOJO getProperties(ResultSet rss) throws SQLException {
		FileSharePOJO rs=new FileSharePOJO();
		
		rs.setId(rss.getInt("id"));
		rs.setTenFile(rss.getString("tenfile"));
		rs.setNguoiUp(rss.getString("nguoiup"));
		rs.setIdNhom(rss.getInt("idnhom"));
		rs.setThoiGian(rss.getTimestamp("thoigian").getTime());
		rs.setPath(rss.getString("path"));
		rs.setGhiChu(rss.getString("ghichu"));
		return rs;
	}

	public int insert(FileSharePOJO file) throws Exception{
		int rs = -1;
		String procName = "spInsertFile";
		String[] input = new String[] { file.getTenFile(),file.getNguoiUp(),file.getIdNhom()+"",file.getPath(),file.getGhiChu() };
		
			ResultSet rss = provider.executeStore(procName, input);
			if (rss.next()) {
				rs = rss.getInt("ID");
				file.setId(rs);
			}		
		return rs;
	}
	public FileSharePOJO get(int id){
		FileSharePOJO rs=null;
		String sql="select * from fileshare where id="+id;
		ResultSet rss=provider.executeQuery(sql);
		try {
			if(rss.next()){
				rs=getProperties(rss);
			}
		} catch (SQLException e) {
			rss=null;
			e.printStackTrace(System.out);
		}
		return rs;
	}
	public ArrayList<FileSharePOJO> getTheoNhom(int idNhom){
		ArrayList<FileSharePOJO> rs=new ArrayList<FileSharePOJO>();
		try{
			String sql="select * from fileshare where idnhom="+idNhom;
			ResultSet rss=provider.executeQuery(sql);
			
			while(rss.next()){
				FileSharePOJO file=getProperties(rss);
				rs.add(file);
			}
		}catch(Exception e){
			rs=null;
			e.printStackTrace(System.out);
		}
		return rs;
	}
	public ArrayList<FileSharePOJO> getTheoNguoiUp(String tenDangNhap){
		ArrayList<FileSharePOJO> rs=null;
		return rs;
	}
}
